package com.allen.app.common.config.session;

import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import com.allen.app.common.utils.CheckObjectUtils;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * sa-token代码方式进行配置
 * @author kong
 *
 */
@Configuration
@EnableConfigurationProperties(SessionProperties.class)
@AllArgsConstructor
@ConditionalOnProperty(prefix = SessionProperties.PREFIX, name = "enable", havingValue = "true")
public class MySaTokenConfig implements WebMvcConfigurer {

	private SessionProperties sessionProperties;

	// 注册sa-token的拦截器，打开注解式鉴权功能
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 注册注解拦截器 
		InterceptorRegistration interceptorRegistration = registry.addInterceptor(new SaRouteInterceptor())
			.addPathPatterns("/**");
		if(CheckObjectUtils.nonNull(sessionProperties.getIgnorePath())){
			interceptorRegistration.excludePathPatterns(sessionProperties.getIgnorePath());
		}
	}
	
}